﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model
{
    /// <summary>
    /// 服务层的返回值基类
    /// </summary>
    public class BaseResult<T>
    {
        /// <summary>
        /// 返回码
        /// </summary>
        public int Code { get; set; }
        /// <summary>
        /// 返回码说明
        /// </summary>
        public string Description { get; set; }
        /// <summary>
        /// 数据
        /// </summary>
        public T Data { get; set; }
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success { get; set; }
    }
    public class BaseResultFactory
    {
        /// <summary>
        /// 创建成功的结果对象
        /// </summary>
        /// <returns></returns>
        public static BaseResult<T> CreateSuccess<T>()
        {
            BaseResult<T> br = new BaseResult<T>();
            br.Code = BaseResultCode.CODE_OK;
            br.Description = "OK";
            br.Success = true;
            return br;
        }
        /// <summary>
        /// 创建成功的结果对象
        /// </summary>
        /// <param name="data">结果中的数据对象</param>
        /// <returns></returns>
        public static BaseResult<T> CreateSuccess<T>(T data)
        {
            BaseResult<T> br = CreateSuccess<T>();
            br.Data = data;
            return br;
        }

        /// <summary>
        /// 创建失败的结果对象
        /// </summary>
        /// <param name="code">错误码</param>
        /// <param name="description">错误描述</param>
        /// <returns></returns>
        public static BaseResult<T> CreateFail<T>(int code, string description)
        {
            BaseResult<T> br = new BaseResult<T>();
            br.Code = code;
            br.Description = description;
            br.Success = false;
            return br;
        }

        /// <summary>
        /// 创建失败的结果对象
        /// </summary>
        /// <param name="code">错误码</param>
        /// <param name="description">错误描述</param>
        /// <returns></returns>
        public static BaseResult<T> CreateFail<T>(int code, string description, T data)
        {
            BaseResult<T> br = CreateFail<T>(code, description);
            br.Data = data;
            return br;
        }
    }
    public class BaseResultCode
    {
        /// <summary>
        /// 处理成功
        /// </summary>
        public static int CODE_OK = 200;
        /// <summary>
        /// 删除数据失败
        /// </summary>
        public static int CODE_DELETE_FAILURE = 501;
        /// <summary>
        /// 更新数据失败
        /// </summary>
        public static int CODE_UPDATE_FAILURE = 502;
        /// <summary>
        /// 添加数据失败
        /// </summary>
        public static int CODE_ADD_FAILURE = 503;
        /// <summary>
        /// 查询数据失败
        /// </summary>
        public static int CODE_SEARCH_FAILURE = 504;
    }
}
